﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

using OpenTK;
using OpenTK.Graphics.OpenGL;
using System.Drawing;

namespace GraphicsF
{
    public static class GF
    {
        public static void ShowOrdenate()
        {
            GL.PointSize(2.0f);
            GL.Color3(Color.Blue);
            GL.Begin(BeginMode.Lines);
            GL.Vertex3(0, 0, 0);
            GL.Vertex3(80, 0, 0);

            GL.Vertex3(80, 0, 0);
            GL.Vertex3(77, 0, 1);

            GL.Vertex3(80, 0, 0);
            GL.Vertex3(77, 0, -1);

            GL.Vertex3(80, 0, 0);
            GL.Vertex3(77, 1, 0);

            GL.Vertex3(80, 0, 0);
            GL.Vertex3(77, -1, 0);

            GL.Vertex3(0, 0, 0);
            GL.Vertex3(-75, 0, 0);

            GL.Color3(Color.Red);
            GL.Vertex3(0, 0, 0);
            GL.Vertex3(0, 0, 80);

            GL.Vertex3(0, 0, 0);
            GL.Vertex3(0, 0, -75);

            GL.Vertex3(0, 0, 80);
            GL.Vertex3(1, 0, 77);

            GL.Vertex3(0, 0, 80);
            GL.Vertex3(-1, 0, 77);

            GL.Vertex3(0, 0, 80);
            GL.Vertex3(0, 1, 77);

            GL.Vertex3(0, 0, 80);
            GL.Vertex3(0, -1, 77);

            GL.Color3(Color.LawnGreen);
            GL.Vertex3(0, 0, 0);
            GL.Vertex3(0, 75, 0);

            GL.Vertex3(0, 75, 0);
            GL.Vertex3(0, 72, 1);

            GL.Vertex3(0, 75, 0);
            GL.Vertex3(0, 72, -1);

            GL.Vertex3(0, 75, 0);
            GL.Vertex3(1, 72, 0);

            GL.Vertex3(0, 75, 0);
            GL.Vertex3(-1, 72, 0);

            GL.End();
        }
        public static void BuildStruct(List<Modeling.Point> points, double wmax, double wmin, double w2, int zoom)
        {
            GL.PointSize(5);
            //Color.from
            foreach (var numbermodel in points.GroupBy(x => x.NumberModel))
                foreach (var frequency in numbermodel.GroupBy(x => x.Theta))
                {
                    GL.Begin(BeginMode.LineStrip);
                    foreach (var point in frequency)
                    {
                        double x = point.K * Math.Cos(point.Theta);
                        double y = point.K * Math.Sin(point.Theta);
                        switch (point.NumberModel)
                        {
                            case 0:
                                if (point.W > w2)
                                    GL.Color3(Color.Red);
                                else
                                    GL.Color3(Color.Blue);
                                break;
                            case 1:
                                if (point.W > w2)
                                    GL.Color3(Color.Yellow);
                                else
                                    GL.Color3(Color.White);
                                break;
                        }
                        if (point.W <= wmax)
                            GL.Vertex3(x / 70.0 / (zoom / 1.1), (point.W - wmin) / Math.Pow(10, zoom / 9.5), y / (zoom / 1.1) / 70.0);
                    }
                    GL.End();
                }
            foreach (var numbermodel in points.GroupBy(x => x.NumberModel))
                for (double theta = Math.PI; theta <= 2 * Math.PI; theta += Math.PI)
                    foreach (var k in numbermodel.GroupBy(x => x.K))
                    {
                        GL.Begin(BeginMode.LineStrip);
                        foreach (var point in k)
                        {
                            double x = point.K * Math.Cos(point.Theta);
                            double y = point.K * Math.Sin(point.Theta);
                            switch (point.NumberModel)
                            {
                                case 0:
                                    if (point.W > w2)
                                        GL.Color3(Color.Red);
                                    else
                                        GL.Color3(Color.Blue);
                                    break;
                                case 1:
                                    if (point.W > w2)
                                        GL.Color3(Color.Yellow);
                                    else
                                        GL.Color3(Color.White);
                                    break;
                            }
                            if (point.W <= wmax && point.Theta <= theta && point.Theta > theta - Math.PI)
                                GL.Vertex3(x / 70.0 / (zoom / 1.1), (point.W - wmin) / Math.Pow(10, zoom / 9.5), y / (zoom / 1.1) / 70.0);
                        }
                        GL.End();
                    }
        }
        public static void ShowLimit(int zoom, double min, double max = 0)
        {
            GL.Color3(Color.White);
            GL.Begin(BeginMode.Lines);
            if (min != 0)
            {
                GL.Vertex3(0, min / Math.Pow(10, zoom / 9.5), -70);
                GL.Vertex3(0, min / Math.Pow(10, zoom / 9.5), 70);
            }
            if (max != 0)
            {
                GL.Vertex3(0, max / Math.Pow(10, zoom / 9.5), -70);
                GL.Vertex3(0, max / Math.Pow(10, zoom / 9.5), 70);
            }            
            GL.End();
        }
        public static void ShowNet()
        {
            GL.Begin(BeginMode.Lines);
            GL.Color3(Color.FromArgb(255, 100, 100, 100));

            for (int i = -75; i < 80; i += 5)
                if (i != 0)
                {
                    GL.Vertex3(0, 0, i);
                    GL.Vertex3(75, 0, i);
                    GL.Vertex3(0, 0, i);
                    GL.Vertex3(-75, 0, i);

                    GL.Vertex3(i, 0, 0);
                    GL.Vertex3(i, 0, 75);
                    GL.Vertex3(i, 0, 0);
                    GL.Vertex3(i, 0, -75);
                }
            GL.End();
        }
    }
}
